
package org.apache.solr.response.transform;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.ReaderUtil;
import org.apache.lucene.queries.function.FunctionValues;
import org.apache.lucene.queries.function.ValueSource;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrException;
import org.apache.solr.search.QParser;
import org.apache.solr.search.SolrIndexSearcher;

/**
 * Add values from a ValueSource (function query etc)
 *
 * NOT really sure how or if this could work...
 * @since solr 4.0
 */
public class ValueSourceAugmenter extends DocTransformer {

    public final String name;
    public final QParser qparser;
    public final ValueSource valueSource;

    public ValueSourceAugmenter(String name, QParser qparser, ValueSource valueSource) {
        this.name = name;
        this.qparser = qparser;
        this.valueSource = valueSource;
    }

    @Override
    public String getName() {
        return "function(" + name + ")";
    }

    @Override
    public void setContext(TransformContext context) {

        try {
            IndexReader reader = qparser.getReq().getSearcher().getIndexReader();
            readerContexts = reader.leaves();
            docValuesArr = new FunctionValues[readerContexts.size()];

            searcher = qparser.getReq().getSearcher();
            fcontext = ValueSource.newContext(searcher);
            this.valueSource.createWeight(fcontext, searcher);
        }
        catch(IOException e) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
        }
    }
    Map fcontext;
    SolrIndexSearcher searcher;
    List<AtomicReaderContext> readerContexts;
    FunctionValues docValuesArr[];

    @Override
    public void transform(SolrDocument doc, int docid) {
        // This is only good for random-access functions

        try {
            // TODO: calculate this stuff just once across diff functions
            int idx = ReaderUtil.subIndex(docid, readerContexts);
            AtomicReaderContext rcontext = readerContexts.get(idx);
            FunctionValues values = docValuesArr[idx];
            if(values == null) {
                docValuesArr[idx] = values = valueSource.getValues(fcontext, rcontext);
            }

            int localId = docid - rcontext.docBase;
            Object val = values.objectVal(localId);
            if(val != null) {
                doc.setField(name, val);
            }
        }
        catch(IOException e) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "exception at docid " + docid + " for valuesource " + valueSource, e);
        }
    }
}
